package com.howard.exception;


import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
 
public class ExceptionInterceptor extends AbstractInterceptor {   
 
	private static final Logger logger = LoggerFactory.getLogger(ExceptionInterceptor.class);   
  
	private static final long serialVersionUID = 1L;   
	
	private static final String ERROR_PAGE="ERROR";
	
	@SuppressWarnings("unchecked")
	@Override	  
	public  String intercept(ActionInvocation invocation) throws Exception{   
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String actionName = invocation.getInvocationContext().getName();   
		String result = null;   
		/*HttpSession session=request.getSession();
		ActionContext context=invocation.getInvocationContext();
		Object action=invocation.getAction();
//		System.out.println("action:"+action.toString());
		
		boolean saveParams=false;
		boolean getParams=false;
		//得到页面的参数
		Map<String, Object> map=context.getParameters();
		Map<String, Object> newMap=new HashMap<String, Object>();
		Iterator it=map.entrySet().iterator();
//		System.out.println("param:{");
		while(it.hasNext()){
			Entry entry=(Entry) it.next();
			if(entry.getKey().equals("saveParams")){
				saveParams=true;
				System.out.println("*****saveParams");
			}
			if(entry.getKey().equals("getParams")){
				getParams=true;
				System.out.println("*****getParams");
			}
			String[] aaa=(String[]) entry.getValue();
			newMap.put(String.valueOf(entry.getKey()), String.valueOf(aaa[0]));
//			System.out.println(entry.getKey()+"="+entry.getValue());
		}
		
		
//		System.out.println("}");s
		if(saveParams) session.setAttribute("last.query.params", newMap);

//		Map<String, Object> map1=new HashMap<String, Object>();
//		map1.put("person.name", "开始了");
//		map1.put("person.age", "10");
		
		
		if(getParams){
			Map map1=(Map) session.getAttribute("last.query.params");
			context.setParameters(map1);
			Iterator it1=map1.entrySet().iterator();
			while(it1.hasNext()){
				Entry entry=(Entry) it1.next();
				System.out.println(entry.getKey()+"="+entry.getValue());
			}
		}
		*/
		
		try{   
			result= invocation.invoke();   
		}/*catch(SocketException ex){   
//			ac.getSession().put("msg", "ConnectException数据库链接异常");
			System.out.println("**********SocketException**********");
			request.setAttribute("msg", "SocketException");
//			logger.error("异常拦截器拦截到异常：SQL语句出错！"+"<br>"+"action的名称为:"+actionName+"<br>"+"异常的详 细信息："+"<br>"+ex.toString());   
			return ERROR_PAGE;
	  
		}catch(UncategorizedSQLException ex){   
			System.out.println("**********UncategorizedSQLException**********");
			request.setAttribute("msg", "UncategorizedSQLException");
//			logger.error("异常拦截器拦截到异常：SQL语句出错！"+"<br>"+"action的名称为:"+actionName+"<br>"+"异常的详 细信息："+"<br>"+ex.toString());   
			return ERROR_PAGE;   
	  
		}catch(SQLException ex){   
			System.out.println("**********SQLException**********");
//			logger.error("异常拦截器拦截到异常：SQL语句出错！"+"<br>"+"action的名称为:"+actionName+"<br>"+"异常的详 细信息："+"<br>"+ex.toString());   
			return ERROR_PAGE;   
	  
		}catch (NullPointerException ex){     
			System.out.println("**********NullPointerException**********");
//			logger.error("异常拦截器拦截到异常：调用了未经初始化的对象或者是不存在的对象！"+"<br>"+"action的名称为:"+actionName+"<br>"+"异常的详细信息："+"<br>"+ex.toString());   
			return "ERROR_PAGE";   
	  
		}catch (IOException ex){   
			System.out.println("**********IOException**********");
//			logger.error("异常拦截器拦截到异常：IO异常！"+"<br>"+"action的名称为:"+actionName+"<br>"+"异常的详细信息："+"<br>"+ex.toString());
			return ERROR_PAGE;
			
		}*/catch (Exception ex) {
			System.out.println("**********Exception**********");
			request.setAttribute("error", "异常拦截器拦截到异常："+"<br>"+"action的名称为:"+actionName+"<br>"+"异常的详细信息："+"<br>"+ex.toString());
			return ERROR_PAGE;
		}
		return result;	
	}
}

